<template>
  <view class="loginLayout sysBg">
    <view class="loginImage">
      <image src="/static/images/token.png" mode="aspectFit"></image>
    </view>
    <view class="form">
      <view class="account">
        <up-input
          type="text"
          prefixIcon="/static/images/account.png"
          placeholder="账号"
          prefixIconStyle="font-size: 22px;color: #909399"
		  v-model="username"
        ></up-input>
      </view>
      <view class="password">
		<up-input
		  :type="showPassword ? 'text' : 'password'"
		  placeholder="密码"
		  prefixIcon="/static/images/password.png"
		  suffixIconStyle="color: #909399"
		  v-model="password"
		>
		<template #suffix>
		  <up-icon
			:name="showPassword ? 'eye-fill' : 'eye-off'"
			@click="showPassword = !showPassword"
			style="margin-left: 8px;"
		  />
		</template>
		</up-input>
      </view>
      <view class="loginbtn">
        <button type="primary" @tap.stop="handleLogin">登录</button>
        <view></view>
        <button type="default" @click="register">注册</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { useUserStore } from "@/stores/user";
import { encode } from "@/utils/tool";

import { userLogin } from "@/api/user/userApi";



const showPassword = ref(false);
const userStore = useUserStore();
const username = ref("");
const password = ref("");


const register = ()=>{
	let aa;
	aa.bb.cc=1;
}


const changePwd = (e)=>{
	console.log('password.value=====',password.value);
}


const handleLogin = async () => {
  const tabBarPage = [
    "/pages/index/index",
    "/pages/category/category",
    "/pages/my/my",
    "/pages/shopping/shopping",
  ];
  

  try {
    const res = await userLogin({
      username: "admin",
      password: "admin",
    });

    userStore.login(res.token); // 更新 Pinia 状态

    // 获取重定向路径，默认跳首页
    let redirectUrl =
      uni.getStorageSync("loginRedirect") || "/pages/index/index";
    //console.log("redirectUrl=====", redirectUrl);
    redirectUrl = decodeURIComponent(redirectUrl);
    if (redirectUrl.startsWith("pages")) {
      redirectUrl = "/" + redirectUrl;
    }
	
    if (tabBarPage.includes(redirectUrl)) {
      uni.reLaunch({
        url: redirectUrl,
      });
    } else {
	  uni.navigateTo({
	    url: redirectUrl,
	  });
    }
    uni.removeStorageSync("loginRedirect"); // 清除临时存储
    return false;
  } catch (error) {
    uni.showToast({
      title: "登录失败",
      icon: "error",
    });
  }
};


//原生标题栏增加分享按钮，点击事件可通过页面的 onNavigationBarButtonTap 函数进行监听

onLoad(async (query) => {
  // 保存redirect参数
  if (query.redirect) {
    let redirect = query.redirect;

    uni.setStorageSync("loginRedirect", redirect);
  }
  
  //这个事件用来监听原生导航上添加的按钮
  onNavigationBarButtonTap(()=>{
  	console.log('原生按钮点击')
  });
  
  
  
  
});
</script>

<style lang="scss" scoped>
.loginLayout {
  padding: 0 30rpx;

  .loginImage {
    width: 100%;
    height: 400rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .form {
    height: 300rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 20rpx auto;

    view {
      width: 100%;
    }
    .loginbtn {
      display: flex;
      justify-content: space-between;

      button {
        width: 40%;
      }

      view {
        flex: 1;
      }
    }
  }
}
</style>
